import {
  createSlice,
  createAsyncThunk
} from "@reduxjs/toolkit";

import {getMovieRequest} from '../../api'


export const getMovieAction = createAsyncThunk("movie/getMovieList", async () => {
  const result = await getMovieRequest()
  return result.data.data.list
})
const movieSlice = createSlice({
  //命名空间 name值作为action type的前缀
  name: "movie",
  // 初始值
  initialState: {
    movieList: [],
  },
  //定义reducer更新状态函数,  组件中dispatch使用的action函数
  reducers: {

  },

  extraReducers: {
    [getMovieAction.fulfilled](prev, action) {
      prev.movieList = action.payload
    },
    [getMovieAction.rejected]() {
      console.log("异步action失败--getMovieAction");
    },
    [getMovieAction.pending]() {
      console.log("异步action正在进行中--getMovieAction");
    },
  }
})

export default movieSlice.reducer